<%@ page language="java" import="com.sap.mw.jco.IFunctionTemplate" pageEncoding="UTF-8" %>
<%@ page import="com.sap.mw.jco.JCO" %>
<%@ page import="com.weaver.integration.datesource.SAPInterationOutUtil" %>
<%@ page import="com.weaver.integration.log.LogInfo" %>
<%@ page import="weaver.conn.BatchRecordSet" %>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="weaver.general.BaseBean" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="java.util.*" %>
<%--获取流水号--%>
<%
    String curYear = "2022";

    BaseBean baseBean = new BaseBean();
    baseBean.writeLog( ">>>>WeaSynSapCronJob>>>>>curYear="+curYear );
    BatchRecordSet batchRecordSet = new BatchRecordSet();
    SAPInterationOutUtil outUtil = new SAPInterationOutUtil();
    JCO.Client myConnection = (JCO.Client)outUtil.getConnection("2", new LogInfo());
    myConnection.connect();
    // 实例名
    JCO.Repository myRepository = new JCO.Repository("Repository",myConnection);
    //设置BAPI函数
    IFunctionTemplate ft = myRepository.getFunctionTemplate("ZCO_ORDER_BUDGET_ACTUAL");
    JCO.Function bapi = ft.getFunction();
    //输入参数和结构处理
    JCO.ParameterList input = bapi.getImportParameterList();
    input.setValue(curYear, "P_GJAHR");

    //执行函数
    myConnection.execute(bapi);
    //输出参数和结构处理
    //JCO.ParameterList out = bapi.getExportParameterList();
    //输出表的处理
    JCO.ParameterList Table00 = bapi.getTableParameterList();
    //返回处理结果以及日志
    List<List<Object>> resultList = new ArrayList<List<Object>>();
    //返回表
    JCO.Table t = Table00.getTable("IT_FILE");
    baseBean.writeLog( "getNumRows="+t.getNumRows());
    for (int i = 0; i < t.getNumRows(); i++) {
        List<Object> list = new ArrayList<>();
        t.setRow(i);
        String AUFNR = (String) t.getValue("AUFNR");
        String KSTAR = (String) t.getValue("KSTAR");
        String wybs = curYear+AUFNR+KSTAR;
        String dataid = hasRepeatData(wybs);
        if("".equals( dataid )){
            list.add(wybs);
            list.add(curYear);
            list.add(t.getValue("AUFNR"));
            list.add(t.getValue("KSTAR"));
            list.add(t.getValue("WKG001"));
            list.add(t.getValue("WKG002"));
            list.add(t.getValue("WKG003"));
            list.add(t.getValue("WKG004"));
            list.add(t.getValue("WKG005"));
            list.add(t.getValue("WKG006"));
            list.add(t.getValue("WKG007"));
            list.add(t.getValue("WKG008"));
            list.add(t.getValue("WKG009"));
            list.add(t.getValue("WKG010"));
            list.add(t.getValue("WKG011"));
            list.add(t.getValue("WKG012"));
            list.add(t.getValue("WPP001"));
            list.add(t.getValue("WPP002"));
            list.add(t.getValue("WPP003"));
            list.add(t.getValue("WPP004"));
            list.add(t.getValue("WPP005"));
            list.add(t.getValue("WPP006"));
            list.add(t.getValue("WPP007"));
            list.add(t.getValue("WPP008"));
            list.add(t.getValue("WPP009"));
            list.add(t.getValue("WPP010"));
            list.add(t.getValue("WPP011"));
            list.add(t.getValue("WPP012"));
            resultList.add(list);
        }else {
            Map map = new HashMap(  );
            map.put("orders",t.getValue("AUFNR"));
            map.put("kmbm", t.getValue("KSTAR"));
            map.put("sjje1yf",t.getValue("WKG001"));
            map.put("sjje2yf", t.getValue("WKG002"));
            map.put("sjje3yf", t.getValue("WKG003"));
            map.put("sjje4yf", t.getValue("WKG004"));
            map.put("sjje5yf", t.getValue("WKG005"));
            map.put("sjje6yf", t.getValue("WKG006"));
            map.put("sjje7yf", t.getValue("WKG007"));
            map.put("sjje8yf", t.getValue("WKG008"));
            map.put("sjje9yf", t.getValue("WKG009"));
            map.put("sjje10yf", t.getValue("WKG010"));
            map.put("sjje11yf", t.getValue("WKG011"));
            map.put("sjje12yf", t.getValue("WKG012"));
            map.put("ysje1yf", t.getValue("WPP001"));
            map.put("ysje2yf", t.getValue("WPP002"));
            map.put("ysje3yf", t.getValue("WPP003"));
            map.put("ysje4yf", t.getValue("WPP004"));
            map.put("ysje5yf", t.getValue("WPP005"));
            map.put("ysje6yf", t.getValue("WPP006"));
            map.put("ysje7yf", t.getValue("WPP007"));
            map.put("ysje8yf", t.getValue("WPP008"));
            map.put("ysje9yf", t.getValue("WPP009"));
            map.put("ysje10yf", t.getValue("WPP010"));
            map.put("ysje11yf", t.getValue("WPP011"));
            map.put("ysje12yf", t.getValue("WPP012"));
            updateModelData("uf_yssjje",dataid,map);
        }
    }
    baseBean.writeLog( "resultList="+resultList.size() );
    if(resultList.size()>0){
        String fields = "wybs,nf,orders,kmbm,sjje1yf,sjje2yf,sjje3yf,sjje4yf,sjje5yf,sjje6yf,sjje7yf,"+
                "sjje8yf,sjje9yf,sjje10yf,sjje11yf,sjje12yf,ysje1yf,ysje2yf,ysje3yf,ysje4yf,ysje5yf," +
                "ysje6yf,ysje7yf,ysje8yf,ysje9yf,ysje10yf,ysje11yf,ysje12yf";
        String sql = "insert into uf_yssjje("+fields+") values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        batchRecordSet.executeBatchSqlNew(sql, resultList);
    }
    JCO.releaseClient(myConnection);
%>
<%!
    public static String hasRepeatData(String wybs){
        RecordSet rs = new RecordSet(  );
        rs.executeQuery("select id from uf_yssjje where wybs=?",wybs);
        rs.next();
        return Util.null2String( rs.getString("id") );
    }

    public static boolean updateModelData(String tablename,String dataid, Map map) {
        RecordSet rs = new RecordSet();
        String sql = "update " + tablename + " set  ";
        String valuesStr = "";
        Iterator<Map.Entry<String, Object>> it = map.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, Object> entry = it.next();
            String key = Util.null2String( entry.getKey() );
            String value = "'"+Util.null2String( entry.getValue() )+"'";
            if (valuesStr.equals( "" )) {
                valuesStr = key + "=" + value + "";
            } else {
                valuesStr += "," + key + "=" + value + "";
            }
        }
        sql += valuesStr + " where id='" + dataid + "'";
        return rs.execute( sql );
    }
%>
